package com.dicsystems.myrec.dao;

import java.util.ArrayList;
import java.util.List;

import com.dicsystems.myrec.DatabaseHelper;
import com.dicsystems.myrec.dto.Category;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CategoryDao {

	private Context me;
	private SQLiteDatabase db;
	private final String TABLE = "category";
	private final int ID = 0;
	private final int NAME = 1;

	public CategoryDao(Context context) {
		this.me = context;
	}

	public void open() {
		db = new DatabaseHelper(me).getReadableDatabase();
	}

	public void close() {
		db.close();
	}

	public List<Category> select() {
		List<Category> categories = new ArrayList<Category>();
		Cursor cursor = db.query(TABLE, null, null, null, null, null, "_id");
		if(cursor.moveToFirst()) {
			do {
				Category category = new Category();
				category.id = cursor.getString(ID);
				category.name = cursor.getString(NAME);
				categories.add(category);
			} while(cursor.moveToNext());
		}
		cursor.close();
		return categories;
	}
}
